<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\User\UserChatReplyAction;
use App\Admin\Repositories\WxChat;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxChatController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxChat(), function (Grid $grid) {
            $grid->simplePaginate();
            $app_notice_assistant_user = Settings::get('app_notice_assistant_user', 0);
            if(_empty_($app_notice_assistant_user)){
                $grid->model()->orderBy('id', 'desc');
            }else{
                $grid->model()->where('user_id', '<>', $app_notice_assistant_user)->orderBy('id', 'desc');
            }

            $grid->column('id')->sortable();

            $grid->quickSearch(['id', 'user_id','object_id','user1.user_name','user2.user_name', 'chat_content'])->placeholder('搜索ID，发起人用户名，对象用户名，聊天内容...')->width(50);

            $grid->column('users1', '头像')->display(function ($v) {
                return '<img src="' . ($this->user1['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user1', '发起')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');

            $grid->column('xxx', ' ')->display(function ($v){
                return '➡️';
            });

            $grid->column('users2', '头像')->display(function ($v) {
                return '<img src="' . ($this->user2['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user2', '对象')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');

            $grid->column('chat_content')->display(function ($v){
                return strip_tags(_mini_phone(_mini_aite_replace(_mini_emoji($v, true), true), true));
            })->limit(20);
            $grid->column('chat_image')->image('', 50, 50)->sortable();
            $grid->column('chat_audio_url')->display(function ($v){
               if($v){
                   return '<audio controls><source src="'.$v.'" type="audio/mp3"></audio>';
               }
               return '';
            });
            $grid->column('is_read')->using([0 => '未读', 1 => '已读'])->label([
                0 => 'primary',
                1 => 'gray'
            ])->sortable();
            $grid->column('chat_state')->using([0 => '正常', 1 => '用户删除'])->label([
                0 => 'primary',
                1 => 'red'
            ])->sortable();
            $grid->column('created_at')->sortable();
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id')->select()->ajax('select/users');
                $filter->equal('object_id')->select()->ajax('select/users');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new UserChatReplyAction())->setKey($this->id));
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxChat(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('object_id');
            $show->field('chat_content');
            $show->field('chat_image');
            $show->field('chat_audio_url');
            $show->field('is_read');
            $show->field('chat_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxChat(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('object_id');
            $form->text('chat_content');
            $form->text('chat_image');
            $form->text('is_read');
            $form->text('chat_state');


            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
